package com.qf.team2.netdisc.controller;

import com.qf.team2.netdisc.common.base.BaseController;
import com.qf.team2.netdisc.common.pojo.entity.Document;
import com.qf.team2.netdisc.common.pojo.vo.RespResultVO;
import com.qf.team2.netdisc.service.DocumentService;
import com.qf.team2.netdisc.service.DocumentUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 文件上传业务接口
 *
 * @author Simon_Wong
 * @date 2021/05/26 10:36
 */
@Controller
@RequestMapping("document")
@Api(tags = "文件相关接口")
public class DocumentUploadController extends BaseController<Object> {
    @Autowired
    private DocumentUploadService documentUploadService;

    @Autowired
    private DocumentService documentService;

    @ResponseBody
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @ApiOperation("文件上传接口")
    /*文件，url，文件大小，userid*/
    public RespResultVO<Object> upload(@ApiParam(value = "文件")
                                       @RequestParam(value = "upload", required = false)
                                       MultipartFile file,
                                       @RequestParam(value = "rootId", required = false)
                                       Integer rootId,
                                       HttpServletRequest request) {
        System.out.println("upload");
        System.out.println(file);
        System.out.println(rootId);
        Document dbDocument = documentService.getDocumentById(rootId);
        String url = dbDocument.getUrl();
        try {
            String code = "";
            Cookie[] cks = request.getCookies();
            //遍历Cookie
            for (Cookie ck : cks) {
                //检索出自己的Cookie
                if ("userId".equals(ck.getName())) {
                    //记录Cookie的值
                    code = ck.getValue();
                    break;
                }
            }
            Integer userId = Integer.parseInt(code);
            Long filesize = file.getSize() / 1024;
            /*Integer userId=1;*/
            //查询容量是否足够和文件是否为空
            documentUploadService.checkUserCapacity(userId, filesize, file);
            //查询文件名字是否重复
            documentUploadService.checkFileName(url, userId, file.getOriginalFilename());
            //String filePath=fileUploadService.checkFilePath(params.getFileId(),params.getUserId());
            Document document = documentUploadService.checkFileUpload(url, file, filesize, userId);
            return successResp(document, "上传成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return failedResp(e.getMessage());

        }

    }
}
